import { ConfigProvider } from 'antd';
import themeObj from '@/consts/theme';
import storejs from 'storejs';

export class Theme {
  
  // 当前主题名称
  themeName = '';

  // 当前主题数据
  themeData = {};

  // 构造器
  constructor(themeName) {
    this.setTheme(themeName);
  }
  // 设置主题
  setTheme(themeName, params= {}) {
    var themeItem = themeObj[themeName];
    if (!themeItem) return;

    // 设置 antd 主题
    ConfigProvider.config({
      theme: themeItem.theme,
    });
    this.themeName = themeName;
    this.themeData = themeItem;

    // 设置css3变量
    Object.keys(themeItem.localC3Var).forEach(key => {
      var val = themeItem.localC3Var[key];
      var rootStyle = document.documentElement.style;
      rootStyle?.setProperty(key, val);
    });

    // 缓存本地
    storejs('themeName', themeName);
  }
}

// 默认主题
var defaultThemeName = Object.keys(themeObj)[0];

var theme = new Theme(storejs('themeName') || defaultThemeName);
export default theme;
